package com.yanqu.road.server.cmd.wingroom.shareevent;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.player.ReturnData;
import com.yanqu.road.entity.wingroom.UserWingRoomData;
import com.yanqu.road.entity.wingroom.UserWingRoomShareEvent;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.wingroom.WingRoomProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.wingroom.WingRoomModule;
import com.yanqu.road.server.manger.wingroom.WingRoomMgr;
import com.yanqu.road.server.pb.WingRoomPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

/**
 * @author ：zfr
 * @date ：2022/2/11 10:26
 */
@Cmd(code = GameProtocol.S_WING_ROOM_GET_OTHER_PLAYER_EVENT_REWARD, desc = "领取别人厢房事件的奖励")
public class GetOtherPlayerEventRewardCmd extends AbstractCommand {
    private WingRoomProto.GetOtherPlayerEventRewardReqMsg reqMsg;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = WingRoomProto.GetOtherPlayerEventRewardReqMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        WingRoomProto.GetOtherPlayerEventRewardRespMsg.Builder respMsg = WingRoomProto.GetOtherPlayerEventRewardRespMsg.newBuilder();

        //先查一下领取次数
        UserWingRoomData wingRoomData = player.getModule(WingRoomModule.class).getWingRoomData();
        if (wingRoomData == null) {
            //所有玩家都能领奖 厢房还未解锁也可以领奖 所以先初始化数据
            wingRoomData = player.getModule(WingRoomModule.class).initWingRoomData();
        }
        if (wingRoomData.getTotalGetEventRewardTimes() >= GameConfig.WINGROOM_FIND_FRIENDS_PICK_UP_OTHER_NUM) {
            respMsg.setRet(GameErrorCode.E_WING_ROOM_GET_EVENT_REWARD_TIMES_LIMIT); //当日领取次数已达上限
            player.sendPacket(Protocol.U_WING_ROOM_GET_OTHER_PLAYER_EVENT_REWARD, respMsg);
            return;
        }

        //本服
        if (GameServer.getInstance().getServerId() == reqMsg.getOtherPlayerServerId()) {
            ReturnData returnData = WingRoomMgr.getOtherPlayerEventReward(player.getUserId(), player.getUserInfo().getNickName(), reqMsg.getOtherPlayerId(), reqMsg.getPos());
            respMsg.setRet(returnData.getRet());
            if (returnData.getRet() == 0) {
                String reward = returnData.getParams().get(0);
                respMsg.setReward(reward);
                wingRoomData.setGetEventRewardTimes(wingRoomData.getGetEventRewardTimes() + 1);
                player.getModule(WingRoomModule.class).userWingRoomDataSync();
                player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.WingRoom, eLogMoneyType.WingRoomGetEventReward);
                player.notifyListener(eGamePlayerEventType.WingRoomGetEventRewardTimes.getValue(),1);
            }
            UserWingRoomShareEvent userEvent = WingRoomMgr.getUserEvent(reqMsg.getOtherPlayerId());
            if (userEvent != null) {
                respMsg.setEvent(WingRoomPb.buildEventTempMsg(userEvent));
            }
            player.sendPacket(Protocol.U_WING_ROOM_GET_OTHER_PLAYER_EVENT_REWARD, respMsg);
            return;
        }
        wingRoomData.setPreGetEventRewardTimes(wingRoomData.getPreGetEventRewardTimes() + 1);
        //请求跨服
        WingRoomProto.GetOtherPlayerEventReqCrossMsg.Builder reqCrossMsg = WingRoomProto.GetOtherPlayerEventReqCrossMsg.newBuilder();
        reqCrossMsg.setPlayerName(player.getUserInfo().getNickName());
        reqCrossMsg.setReqMsg(reqMsg);
        player.sendPacket(CrossProtocol.C_WING_ROOM_GET_OTHER_PLAYER_EVENT_REWARD, reqCrossMsg);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
